package com.gitee.dolt.log;


import com.gitee.dolt.log.aspect.SysLogAspect;
import com.gitee.dolt.log.event.OptLogDbHandler;
import com.gitee.dolt.log.event.OptLogListener;
import com.gitee.dolt.log.properties.OptLogProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * 日志自动配置
 * <p>
 * 启动条件：
 * 1，存在web环境
 * 2，配置文件中dolt.log.enabled=true 或者 配置文件中不存在：dolt.log.enabled 值
 *
 * @author LYH
 */
@RequiredArgsConstructor
@EnableAsync
@Configuration
@EnableConfigurationProperties(OptLogProperties.class)
@ConditionalOnWebApplication
@ConditionalOnProperty(prefix = OptLogProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
@Slf4j
public class LogAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public SysLogAspect sysLogAspect() {
        return new SysLogAspect();
    }

//    @Bean
//    @ConditionalOnMissingBean
//    public ActionLoggingAspect actionLoggingAspect() {
//        return new ActionLoggingAspect();
//    }


    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnExpression("${dolt.log.enabled:true}")
    public OptLogListener optLogListener(@Autowired(required = false) OptLogDbHandler optLogDbHandler,
                                         OptLogProperties optLogProperties) {
        return new OptLogListener(optLogDbHandler, optLogProperties);
    }
}
